import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_utils/flutter_utils.dart';
import 'package:flutter_widget/src/config/color_define.dart';
import 'package:flutter_widget/src/config/font.dart';
import 'package:flutter_widget/src/widget/effect/border_widget.dart';
import 'package:flutter_widget/src/widget/effect/card_widget.dart';
import 'package:flutter_widget/src/widget/effect/clip_widget.dart';
import 'package:flutter_widget/src/widget/tap_widget.dart';
import 'package:flutter_widget/src/widget/text/text_format.dart';

extension DialogUtil2 on DialogUtil {
  /// 弹窗选项，建议改使用Alert.show
  /// title,标题
  /// leftText, 左侧按键，不传默认 取消
  /// rightText, 右侧按键，不传默认 确定
  /// onlyRight, 只有一个确定按键
  static Future popAlert({
    required String? title,
    required BuildContext context,
    String? leftText,
    String? rightText,
    Function? leftAction,
    Function? rightAction,
    bool? onlyRight,
  }) {
    return DialogUtil.popCenterDialog(
      context: context,
      useRootNavigator: false,
      barrierDismissible: false,
      barrierColor: FWColorPicker.black.withOpacity(0.4),
      widget: CardWidget(
        radius: 11,
        child: Container(
          width: Screens.width - 90,
          color: FWColorPicker.white,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Padding(
                padding: const EdgeInsets.only(top: 20, left: 20, right: 20),
                child: TextFormatWidget(
                  title ?? "",
                  fontSize: 15,
                  color: FWColorPicker.colorFF333333,
                  height: 1.6,
                  maxLine: 0,
                  textAlign: TextAlign.center,
                  fontWeight: FontWeight.w400,
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(
                    bottom: 15, left: 15, right: 15, top: 20),
                child: SizedBox(
                  height: 40,
                  child: Row(
                    children: onlyRight == true
                        ? [
                            Expanded(
                              child: TapWidget(
                                onTap: () {
                                  Navigator.pop(context);
                                  if (rightAction != null) {
                                    rightAction();
                                  }
                                },
                                child: ClipWidget(
                                  radius: 2,
                                  // height: 40,
                                  child: Container(
                                    color: FWColorPicker.colorFFDAB27E,
                                    // formColor: ColorPicker.colorFFBD312D,
                                    // toColor: ColorPicker.colorFF992024,
                                    child: Center(
                                      child: TextFormatWidget(
                                        rightText ?? "确认",
                                        fontSize: 15,
                                        color: FWColorPicker.white,
                                        fontWeight: FontWeight.w500,
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                            ),
                          ]
                        : [
                            Expanded(
                              child: TapWidget(
                                onTap: () {
                                  Navigator.pop(context);
                                  if (leftAction != null) {
                                    leftAction();
                                  }
                                },
                                child: BorderWidget(
                                  radius: 2,
                                  borderWidth: 0.5,
                                  borderColor: FWColorPicker.colorFFDAB27E,
                                  height: 40,
                                  child: Center(
                                    child: TextFormatWidget(
                                      leftText ?? "取消",
                                      fontSize: 15,
                                      color: FWColorPicker.colorFFA98A62,
                                      fontWeight: FontWeight.w500,
                                    ),
                                  ),
                                ),
                              ),
                            ),
                            const Padding(padding: EdgeInsets.only(left: 15)),
                            Expanded(
                              child: TapWidget(
                                onTap: () {
                                  Navigator.pop(context);
                                  if (rightAction != null) {
                                    rightAction();
                                  }
                                },
                                child: ClipWidget(
                                  radius: 2,
                                  // height: 40,
                                  child: Container(
                                    color: FWColorPicker.colorFFDAB27E,
                                    // formColor: ColorPicker.colorFFBD312D,
                                    // toColor: ColorPicker.colorFF992024,
                                    child: Center(
                                      child: TextFormatWidget(
                                        rightText ?? "确认",
                                        fontSize: 15,
                                        color: FWColorPicker.white,
                                        fontWeight: FontWeight.w500,
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                            )
                          ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  /// 弹窗规则的样式
  /// 有标题和内容的样式，只有一个按键
  static Future popRuleStyle({
    String? title,
    required BuildContext context,
    String? content,
    String? buttonText,
    String? tip,
    Function? tipAction,
    Function? buttonAction,
    double? minHeight,
    TextAlign? textAlign,
    bool? isCenter = false,
  }) {
    return DialogUtil.popCustom(
      context: context,
      useRootNavigator: false,
      barrierDismissible: false,
      barrierColor: FWColorPicker.black.withOpacity(0.4),
      dialog: Dialog(
        backgroundColor: FWColorPicker.transparent,
        // backgroundColor: ColorPicker.white,
        child: CardWidget(
          radius: 11,
          child: Container(
            width: Screens.width - 90,
            color: FWColorPicker.white,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                if (title != null)
                  Padding(
                    padding:
                        const EdgeInsets.only(top: 29, left: 20, right: 20),
                    child: Center(
                      child: TextFormatWidget(
                        title,
                        fontSize: 15,
                        color: content != null
                            ? FWColorPicker.colorFFA98A62
                            : FWColorPicker.colorFF333333,
                        height: 1.6,
                        maxLine: 1,
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                  ),
                if (content != null)
                  Container(
                    padding:
                        const EdgeInsets.only(left: 20, right: 20, top: 15),
                    constraints: BoxConstraints(
                      minHeight: minHeight ?? 100,
                      maxHeight: AdaptUtil.fitSize(300),
                    ),
                    child: SingleChildScrollView(
                      child: isCenter == true
                          ? Center(
                              child: TextFormatWidget(
                                content,
                                fontSize: 15,
                                color: FWColorPicker.colorFF333333,
                                height: 1.6,
                                maxLine: 0,
                                textAlign: textAlign ?? TextAlign.left,
                                fontWeight: FontWeight.w400,
                              ),
                            )
                          : TextFormatWidget(
                              content,
                              fontSize: 15,
                              color: FWColorPicker.colorFF333333,
                              height: 1.6,
                              maxLine: 0,
                              textAlign: textAlign ?? TextAlign.left,
                              fontWeight: FontWeight.w400,
                            ),
                    ),
                  ),
                if (tip != null)
                  Padding(
                    padding: const EdgeInsets.only(
                        left: 20, right: 20, top: 15, bottom: 9),
                    child: TapWidget(
                      onTap: () {
                        if (tipAction != null) {
                          tipAction();
                        }
                      },
                      child: TextFormatWidget(
                        tip,
                        fontSize: 15,
                        color: FWColorPicker.colorFFA98A62,
                      ),
                    ),
                  ),
                Padding(
                  padding: const EdgeInsets.only(
                      bottom: 15, left: 15, right: 15, top: 21),
                  child: TapWidget(
                    onTap: () {
                      Navigator.pop(context);
                      if (buttonAction != null) {
                        buttonAction();
                      }
                    },
                    child: ClipWidget(
                      radius: 2,
                      child: Container(
                        height: 36,
                        color: FWColorPicker.colorFFDAB27E,
                        alignment: Alignment.center,
                        child: TextFormatWidget(
                          buttonText ?? "确认",
                          fontSize: 15,
                          color: FWColorPicker.white,
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  static showDeleteProduct(BuildContext context, Function onDelete) {
    showCupertinoModalPopup(
      context: context,
      builder: (context) {
        return CupertinoActionSheet(
          actions: <Widget>[
            CupertinoActionSheetAction(
              onPressed: () {
                Navigator.pop(context);
                onDelete();
              },
              child: TextFormatWidget(
                '删除',
                fontSize: 17,
                fontWeight: FWFontTool.medium,
                color: Colors.redAccent,
              ),
            ),
          ],
          cancelButton: CupertinoActionSheetAction(
            //取消按钮
            onPressed: () {
              Navigator.pop(context);
            },
            child: TextFormatWidget(
              '取消',
              fontSize: 17,
              fontWeight: FWFontTool.medium,
              color: Colors.blueAccent,
            ),
          ),
        );
      },
    );
  }

  static Future popPersonal({
    String? title,
    required BuildContext context,
    String? content,
    String? buttonText,
    Widget? contentWidget,
    double? minHeight,
    TextAlign? textAlign,
    bool? isCenter = false,
    bool? isWidget = false,
    String? leftText,
    String? rightText,
    Function? leftAction,
    Function? rightAction,
    bool? onlyRight,
  }) {
    return DialogUtil.popCustom(
      context: context,
      useRootNavigator: false,
      barrierDismissible: false,
      barrierColor: FWColorPicker.black.withOpacity(0.4),
      dialog: Dialog(
        backgroundColor: FWColorPicker.transparent,
        // backgroundColor: ColorPicker.white,
        child: CardWidget(
          radius: 11,
          child: Container(
            width: Screens.width - 90,
            color: FWColorPicker.white,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                if (title != null)
                  Padding(
                    padding:
                        const EdgeInsets.only(top: 29, left: 20, right: 20),
                    child: Center(
                      child: TextFormatWidget(
                        title,
                        fontSize: 16,
                        color: content != null
                            ? FWColorPicker.colorFFA98A62
                            : FWColorPicker.colorFF333333,
                        height: 1.6,
                        maxLine: 1,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                if (contentWidget != null && isWidget == true)
                  Container(
                    padding:
                        const EdgeInsets.only(left: 20, right: 20, top: 15),
                    constraints: BoxConstraints(
                      minHeight: minHeight ?? 100,
                      maxHeight: AdaptUtil.fitSize(300),
                    ),
                    child: SingleChildScrollView(child: contentWidget),
                  ),
                if (content != null)
                  Container(
                    padding:
                        const EdgeInsets.only(left: 20, right: 20, top: 15),
                    constraints: BoxConstraints(
                      minHeight: minHeight ?? 100,
                      maxHeight: AdaptUtil.fitSize(300),
                    ),
                    child: SingleChildScrollView(
                      child: isCenter == true
                          ? Center(
                              child: TextFormatWidget(
                                content,
                                fontSize: 15,
                                color: FWColorPicker.colorFF333333,
                                height: 1.6,
                                maxLine: 0,
                                textAlign: textAlign ?? TextAlign.left,
                                fontWeight: FontWeight.w400,
                              ),
                            )
                          : TextFormatWidget(
                              content,
                              fontSize: 15,
                              color: FWColorPicker.colorFF333333,
                              height: 1.6,
                              maxLine: 0,
                              textAlign: textAlign ?? TextAlign.left,
                              fontWeight: FontWeight.w400,
                            ),
                    ),
                  ),
                Padding(
                  padding: const EdgeInsets.only(
                      bottom: 15, left: 15, right: 15, top: 21),
                  child: SizedBox(
                    height: 40,
                    child: Row(
                      children: onlyRight == true
                          ? [
                              Expanded(
                                child: TapWidget(
                                  onTap: () {
                                    Navigator.pop(context);
                                    if (rightAction != null) {
                                      rightAction();
                                    }
                                  },
                                  child: ClipWidget(
                                    radius: 2,
                                    // height: 40,
                                    child: Container(
                                      color: FWColorPicker.colorFFDAB27E,
                                      // formColor: ColorPicker.colorFFBD312D,
                                      // toColor: ColorPicker.colorFF992024,
                                      child: Center(
                                        child: TextFormatWidget(
                                          rightText ?? "确认",
                                          fontSize: 15,
                                          color: FWColorPicker.white,
                                          fontWeight: FontWeight.w500,
                                        ),
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                            ]
                          : [
                              Expanded(
                                child: TapWidget(
                                  onTap: () {
                                    Navigator.pop(context);
                                    if (leftAction != null) {
                                      leftAction();
                                    }
                                  },
                                  child: BorderWidget(
                                    radius: 2,
                                    borderWidth: 0.5,
                                    borderColor: FWColorPicker.colorFFDAB27E,
                                    height: 40,
                                    child: Center(
                                      child: TextFormatWidget(
                                        leftText ?? "取消",
                                        fontSize: 15,
                                        color: FWColorPicker.colorFFA98A62,
                                        fontWeight: FontWeight.w500,
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                              const Padding(padding: EdgeInsets.only(left: 15)),
                              Expanded(
                                child: TapWidget(
                                  onTap: () {
                                    Navigator.pop(context);
                                    if (rightAction != null) {
                                      rightAction();
                                    }
                                  },
                                  child: ClipWidget(
                                    radius: 2,
                                    // height: 40,
                                    child: Container(
                                      color: FWColorPicker.colorFFDAB27E,
                                      // formColor: ColorPicker.colorFFBD312D,
                                      // toColor: ColorPicker.colorFF992024,
                                      child: Center(
                                        child: TextFormatWidget(
                                          rightText ?? "确认",
                                          fontSize: 15,
                                          color: FWColorPicker.white,
                                          fontWeight: FontWeight.w500,
                                        ),
                                      ),
                                    ),
                                  ),
                                ),
                              )
                            ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
